-- NewRareInfoView

local panelWidth = 0 -- panel宽
local panelContentWidth = 0 -- listview宽
local widthOfEachLine = 0 -- 描述文字宽 = listview宽
local wordsNumOfEachLine = 16 -- 一行最多中文字符数
local heightOfEachLine = 0 -- 一行文字高度
local MAX_WIDTH = 290

local RowSpacing = 10 -- panelContent的行间距、Panel_2/line 距离它下面的文字高度
local panelContentBoundaryHeight = 15 -- listView距离 Panel 上、下边界距离

local lineContentSize = cc.size( 306, 2 )
local BoxTypeByIndex = {[3] = Enums.BoxType.pvp, [4] = Enums.BoxType.eventBox, [102] = Enums.BoxType.dungeon}

local ClassRef = app.mvc.createPanelClass(...)

function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewRareInfoNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self._panel = self._ui["Panel"]
	panelWidth = self._panel:getContentSize2()
	self._panelBg = self._ui["Panel"]:getChildByName( "bg" )
	self._panelContent = self._ui["Panel"]:getChildByName( "Panel_Content" )
	panelContentWidth = self._panelContent:getContentSize2()
	widthOfEachLine = panelContentWidth

	self._panel_1 = self._panelContent:getChildByName( "Panel_1" )
	self._panel_1_name = self._panel_1:getChildByName( "name" )
	self._panel_1_desc_1 = self._panel_1:getChildByName( "desc_1" )
	heightOfEachLine = self._panel_1_desc_1:getContentSize().height

	self._panel_2 = self._panelContent:getChildByName( "Panel_2" )
	self._panel_2_line = self._panel_2:getChildByName( "line" )
	self._panel_2_text_1 = self._panel_2:getChildByName( "userContent_1" )
	self._panel_2_text_2 = self._panel_2:getChildByName( "userContent_2" )

	self._panel_3 = self._panelContent:getChildByName( "Panel_3" )
	self._panel_3_line = self._panel_3:getChildByName( "line" )
	self._panel_3_text_1 = self._panel_3:getChildByName( "userContent_1" )
	self._panel_3_text_2 = self._panel_3:getChildByName( "userContent_2" )

	self._panel_1_desc_1:getVirtualRenderer():setMaxLineWidth(MAX_WIDTH)
	self._panel_2_text_1:getVirtualRenderer():setMaxLineWidth(MAX_WIDTH)
	self._panel_2_text_2:getVirtualRenderer():setMaxLineWidth(MAX_WIDTH)
	self._panel_3_text_1:getVirtualRenderer():setMaxLineWidth(MAX_WIDTH)
	self._panel_3_text_2:getVirtualRenderer():setMaxLineWidth(MAX_WIDTH)


	self._panel_4 = self._panelContent:getChildByName( "Panel_4" )
	self._panel_4:setVisible(false)
end

-- data = restrictedreward_show 表中项
function ClassRef:refresh()
	if self._data then
		self:setVisible( true )

		self._panel_1_name:setString( L(self._data.name) )
		self._panel_1_desc_1:setString( L(self._data.string1) )

		------------------------------------>>>>>>>>> 第一部分信息
		local height_1 = 0
		if self._data.string1 then
			-- height_1 = math.ceil( string.utf8len( L(self._data.string1) ) / wordsNumOfEachLine ) * heightOfEachLine
			height_1 = self._panel_1_desc_1:getContentSize().height
		end
		self._panel_1_desc_1:setContentSize( cc.size( widthOfEachLine, height_1 ) )

		local panel_1_height = 0
		if 0 == height_1 then -- 不存在描述
			panel_1_height = self._panel_1_name:getContentSize().height
		else
			panel_1_height = self._panel_1_name:getContentSize().height + RowSpacing/2 + height_1
		end
		-- print( " --------------------------->>>>>> panel_1_height: " .. panel_1_height )
		self._panel_1:setContentSize( cc.size( panelContentWidth, panel_1_height ) )
		self._panel_1_name:setPositionY( panel_1_height )
		self._panel_1_desc_1:setPositionY( panel_1_height - self._panel_1_name:getContentSize().height - RowSpacing/2 )

		------------------------------------>>>>>>>>> 第二部分信息
		if self._data.numshow1 and self._data.numshow2 then -- 2条描述
			self._panel_2_text_1:setVisible( true )
			self._panel_2_text_2:setVisible( true )
		elseif not self._data.numshow1 and not self._data.numshow2 then -- 0条描述
			self._panel_2_text_1:setVisible( false )
			self._panel_2_text_2:setVisible( false )
		else -- 1条描述
			self._panel_2_text_1:setVisible( true )
			self._panel_2_text_2:setVisible( false )
		end

		if 1 == tonumber( self._data.ID ) then -- 每日任务礼盒
			local rewardNumOfDaylyTask = TaskManager:getDailyTaskRewardNum() -- 每日任务可以领取而没领取的数量
			self._panel_2_text_1:setString( string.format( L(self._data.numshow1), rewardNumOfDaylyTask) )

			local canUnlock = TaskManager:getDailyTaskAwardBoxCount()
			self._panel_2_text_2:setString( string.format( L(self._data.numshow2), canUnlock ) )
		elseif 2 == tonumber( self._data.ID ) then -- 地下城经验卡
			local numOfCanReceived = 0
			local numOfSotre = 0
			local dungeonsRewardInfo = UD:getUserExpCard()
			if dungeonsRewardInfo then
				numOfCanReceived = dungeonsRewardInfo:getAllCanReceivedCount()
				for i = 1, 3 do
					numOfSotre = numOfSotre + dungeonsRewardInfo:getCumulationInfoByLevel( i )
				end
			else
				numOfCanReceived = 0
				numOfSotre = 3
			end
			self._panel_2_text_1:setString( string.format( L(self._data.numshow1), numOfCanReceived ) )
			self._panel_2_text_2:setString( string.format( L(self._data.numshow2), numOfSotre ) )
		elseif 3 == tonumber( self._data.ID )
				or 4 == tonumber( self._data.ID )
				or 102 == tonumber( self._data.ID ) then -- 竞技宝箱/活动副本宝箱/地城宝箱
			local maxNum1 = UD:getLuckBoxDailyMaxNum(BoxTypeByIndex[tonumber( self._data.ID )])
			local num1 = UD:getLuckBoxRestTimesByType(BoxTypeByIndex[tonumber( self._data.ID )])
			local str = table.concat( {num1, "/", maxNum1} )
			self._panel_2_text_1:setString( string.format( L(self._data.numshow1), str ) )
			local maxNum2 = UD:getLuckBoxWeeklyMaxNum(BoxTypeByIndex[tonumber( self._data.ID )])
			local num2 = UD:getLuckBoxWeeklyRestTimesByType(BoxTypeByIndex[tonumber( self._data.ID )])
			local str2 = table.concat( {num2, "/", maxNum2} )
			self._panel_2_text_2:setString( string.format( L(self._data.numshow2), str2) )
			--[[ local maxNum = tonumber( GD:getSystemPar( "tourBoxOpenLimited", 10 ) ) -- 每日开启上限
			local str = table.concat( { UD:getLuckBoxRestTimesByType(2), "/", maxNum } )
			self._panel_2_text_1:setString( string.format( L(self._data.numshow1), str ) )

			local maxNumCanHold = tonumber( GD:getSystemPar( "tourBoxAccumulateLimited", 10 ) ) -- 可持有上限
			local haveNum = app:sendMsg("PvpModel", "getPvpBoxNum")
			local str2 = table.concat( { haveNum, "/", maxNumCanHold } )
			self._panel_2_text_2:setString( string.format( L(self._data.numshow2), str2) )
			if haveNum > 0 then
				self._ui["Panel/Panel_Content/Panel_4/name"]:setString(L("ActivictyTreasureTips"))
			else
				self._ui["Panel/Panel_Content/Panel_4/name"]:setString(L("DungeonTreasureTips"))
			end ]]

		--[[ elseif 4 == tonumber( self._data.ID ) then -- 活动副本宝箱
			local maxNum = tonumber( GD:getSystemPar( "eventBoxDailyOpenLimited", 10 ) ) -- 每日开启上限
			local str = table.concat( { UD:getLuckBoxRestTimesByType(3), "/", maxNum } )
			self._panel_2_text_1:setString( string.format( L(self._data.numshow1), str ) )

			local curActivityDungeonsPoint = UD:getActivityDungeonsPoint() -- 活动点数
			local oneBoxNeedPoint = GD:getSystemPar( "eventBoxPointRequired", 200 ) -- 一个包厢需要的点数
			local curAccumulateRewardNum = math.floor( curActivityDungeonsPoint/oneBoxNeedPoint ) -- 累积的个数
			local maxAccumulateRewardNum = tonumber( GD:getSystemPar( "eventBoxAccumulateLimited", 20 ) ) -- 累积的上限
			local str2 = table.concat( { curAccumulateRewardNum, "/", maxAccumulateRewardNum } )
			self._panel_2_text_2:setString( string.format( L(self._data.numshow2), str2) )

			if curAccumulateRewardNum > 0 then
				self._ui["Panel/Panel_Content/Panel_4/name"]:setString(L("ActivictyTreasureTips"))
			else
				self._ui["Panel/Panel_Content/Panel_4/name"]:setString(L("DungeonTreasureTips"))
			end ]]
		elseif 101 == tonumber( self._data.ID ) then -- 好运钥匙
			local ownNum, usedToday = UD.player:getCharmUsedInfo()
			local canUsedToday = tonumber( GD:getSystemPar( "charmdailymaxcostamount" ) ) -- 今日可使用上限
			local maxNum = tonumber( GD:getSystemPar( "charmmaxamount" ) ) -- 可持有上限

			self._panel_2_text_1:setString( string.format( L(self._data.numshow1), ( canUsedToday - usedToday ), canUsedToday ) )
			self._panel_2_text_2:setString( string.format( L(self._data.numshow2), ownNum ) )
		--[[ elseif 102 == tonumber( self._data.ID ) then -- 地城宝箱
			local maxNum = tonumber( GD:getSystemPar( "dungeonBoxOpenLimited", 10 ) ) -- 每日开启上限
			local str = table.concat( { UD:getLuckBoxRestTimesByType(1), "/", maxNum } )
			self._panel_2_text_1:setString( string.format( L(self._data.numshow1), str ) )
			self._ui["Panel/Panel_Content/Panel_4/name"]:setString(L("DungeonTreasureTips"))
			 ]]
		end

		local panel_2_height = 0
		if self._data.numshow1 and self._data.numshow2 then -- 2条描述
			panel_2_height = lineContentSize.height + RowSpacing + heightOfEachLine + RowSpacing/2 + heightOfEachLine
		elseif not self._data.numshow1 and not self._data.numshow2 then -- 0条描述
			panel_2_height = 0
		else -- 1条描述
			panel_2_height = lineContentSize.height + RowSpacing + heightOfEachLine
		end
		-- print( " --------------------------->>>>>> panel_2_height: " .. panel_2_height )
		if 0 == panel_2_height then
			self._panel_2:setVisible( false )
		else
			self._panel_2:setVisible( true )
		end
		self._panel_2:setContentSize( cc.size( panelContentWidth, panel_2_height ) )
		self._panel_2_line:setPositionY( panel_2_height )
		self._panel_2_text_1:setPositionY( panel_2_height - lineContentSize.height - RowSpacing )
		self._panel_2_text_2:setPositionY( panel_2_height - lineContentSize.height - RowSpacing - heightOfEachLine - RowSpacing/2 )

		------------------------------------>>>>>>>>> 第三部分信息
		self._panel_3_text_1:setString( L(self._data.string3) )
		self._panel_3_text_2:setString( L(self._data.string4) )
		local panel_3_height = 0
		if self._data.string3 and self._data.string4 then -- 2条描述
			-- local height_3 = math.ceil( string.utf8len( L(self._data.string3) ) / wordsNumOfEachLine ) * heightOfEachLine
			-- local height_4 = math.ceil( string.utf8len( L(self._data.string4) ) / wordsNumOfEachLine ) * heightOfEachLine
			local height_3 = self._panel_3_text_1:getContentSize().height
			local height_4 = self._panel_3_text_2:getContentSize().height
			panel_3_height = lineContentSize.height + RowSpacing + height_3 + RowSpacing/2 + height_4
		elseif not self._data.string3 and not self._data.string4 then -- 0条描述
			panel_3_height = 0
		else -- 1条描述
			if self._data.string3 then
				panel_3_height = lineContentSize.height + RowSpacing + self._panel_3_text_1:getContentSize().height
			else
				panel_3_height = lineContentSize.height + RowSpacing + self._panel_3_text_2:getContentSize().height
			end
		end
		-- print( " --------------------------->>>>>> panel_3_height: " .. panel_3_height )
		if 0 == panel_3_height then
			self._panel_3:setVisible( false )
		else
			self._panel_3:setVisible( true )
		end
		self._panel_3:setContentSize( cc.size( panelContentWidth, panel_3_height ) )
		self._panel_3_line:setPositionY( panel_3_height - lineContentSize.height )

		-- self._panel_3_text_1:setContentSize( cc.size( widthOfEachLine, math.ceil( string.utf8len( L(self._data.string3) ) / wordsNumOfEachLine ) * heightOfEachLine ) )
		self._panel_3_text_1:setPositionY( panel_3_height - lineContentSize.height - RowSpacing )

		-- self._panel_3_text_2:setContentSize( cc.size( widthOfEachLine, math.ceil( string.utf8len( L(self._data.string4) ) / wordsNumOfEachLine ) * heightOfEachLine ) )
		self._panel_3_text_2:setPositionY( panel_3_height - lineContentSize.height - RowSpacing - self._panel_3_text_1:getContentSize().height - RowSpacing/2 )

		local panel_4_height = 0
		-- 快捷方式
		if self._quickJump then
			local rareId = tonumber( self._data.ID )
			if 102 == rareId or 3 == rareId or  4 == rareId then
				self._panel_4:setVisible(true)
				panel_4_height = self._panel_4:getContentSize().height
				self._ui["Panel/Panel_Content/Panel_4/btn_gain"]:addClickEventListener(function()
					local data = GD:queryRestrictedRewardShowById( rareId )
					if not data then
						return
					end
					local featureonConf = GD:queryFeatureOnById( data.point )
					if featureonConf then
						if featureonConf.questId and "" ~= featureonConf.questId then -- 任务控制？
							if UD:isMainTaskFinished( featureonConf.questId ) then -- 任务完成？
								executeFunc(self._onOperate, "jumpto")
								UIHandler:handleMessage({id = tonumber( data.point ), mode = "open"})
							else
								local taskConf =  GD:queryTaskById( featureonConf.questId )
								local str = string.format( L("afterFinishTask"), L(taskConf.name) )
								display.pushToast( str )
							end
						else
							if tonumber( featureonConf.level ) >= UD:getLevel() then
								display.pushToast( L("ErrPlayerLvNotEnough") )
							else
								executeFunc(self._onOperate, "jumpto")
								UIHandler:handleMessage({id = tonumber( data.point ), mode = "open"})
							end
						end
					else
						print( " ------------------------>>>>> 宝物跳转功能Id对应配置不存在 ", data.point )
					end
				end)
			else
				self._panel_4:setVisible(false)
			end
		end
		------------------------------------>>>>>>>>> 调整高度
		local sumHeight = panel_1_height + panel_2_height + panel_3_height + panel_4_height
		if 0 ~= panel_2_height then
			sumHeight = sumHeight + RowSpacing
		end
		if 0 ~= panel_3_height then
			sumHeight = sumHeight + RowSpacing
		end

		local panelSumHeight = sumHeight + panelContentBoundaryHeight * 2
		if 0 ~= panel_4_height then
			sumHeight = sumHeight + RowSpacing
			panelSumHeight = sumHeight + panelContentBoundaryHeight + 4
		end

		-- print( " --------------------------->>>>>> listHeight: " .. sumHeight )
		self._panelContent:setContentSize( cc.size( panelContentWidth, sumHeight ) )
		self._panel_1:setPositionY( sumHeight )
		self._panel_2:setPositionY( sumHeight - panel_1_height - RowSpacing )
		if 0 ~= panel_2_height then
			self._panel_3:setPositionY( sumHeight - panel_1_height - RowSpacing - panel_2_height - RowSpacing )
		else
			self._panel_3:setPositionY( sumHeight - panel_1_height - RowSpacing )
		end
		self._panel_4:setPositionY(panel_4_height)

		-- print( " --------------------------->>>>>> panelSumHeight: " .. panelSumHeight )
		self._panel:setContentSize( cc.size( panelWidth, panelSumHeight ) )
		self:setContentSize( cc.size( panelWidth, panelSumHeight ) )

		self._panelBg:setContentSize( cc.size( panelWidth, panelSumHeight ) )
		self._panelBg:setPositionY( panelSumHeight )

		self._panelContent:setPositionY( panelSumHeight - panelContentBoundaryHeight )

		self._panel:forceDoLayout()
	else
		self:setVisible( false )
	end
end

function ClassRef:returnContentHeight()
	return self._panel:getContentSize().height
end

function ClassRef:onOperate(cb)
	self._onOperate = cb
end

function ClassRef:setQuickJumpEnable(trueOrFasle)
	self._quickJump = trueOrFasle
end

return ClassRef
